#include<iostream>
#include<arpa/inet.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<cstring>
#include<sys/types.h>

// ./tcpclient serverip serverport

void Usage(const std::string& proc)
{
    std::cout<<"\n\rUsage"<<proc<<"serverip  serverport\n"<<std::endl;
}
int main(int argc,char* argv[])
{
    if(argc != 3)
    {
        Usage(argv[0]);
        exit(1);
    }
    std::string serverip = argv[1];
    uint16_t serverport = std::stoi(argv[2]);

    struct sockaddr_in server;
    memset(&server,0,sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(serverport);
    inet_pton(AF_INET,serverip.c_str(),&(server.sin_addr));

    

    //1.create
    while(true)
    {
        int sockfd = 0;
        int cnt = 5;
        int isreconnect = false;

        sockfd = socket(AF_INET,SOCK_STREAM,0);
        if(sockfd < 0)
        {
            std::cerr<<"socket error"<<std::endl;
            return 1;
        }

        do
        {
            //2.tcp客户端要绑定,但不是显示的绑定,而是系统进行bind,随机端口
            //客户端发起connect的时候绑定
        
            int n = connect(sockfd,(struct sockaddr*)&server,sizeof(server));
            if(n < 0)
            {
                isreconnect = true;
                cnt--;
                std::cerr<<"connect error...,reconnect:"<<cnt<<std::endl;
                sleep(1);
            }
            else
            {
                break;
            }
        }while(cnt && isreconnect);
        
        if(cnt == 0)
        {
            std::cerr<<"user offline"<<std::endl;
            break;
        }

        // while(true)
        // {
            std::string message;
            std::cout<<"Please Enter# "<<std::endl;
            std::getline(std::cin,message);

            int n = write(sockfd,message.c_str(),message.size());
            if(n < 0)
            {
                std::cerr<<"write error..."<<std::endl;
                //break;
            }
            char inbuffer[4096];



            n = read(sockfd,inbuffer,sizeof(inbuffer));
            if(n > 0)
            {
                inbuffer[n] = 0;
                std::cout<<inbuffer<<std::endl;
            }
            else
            {
                //break;
            }

            close(sockfd);
        //}
        
    }
    return 0;
}